package com.gs.wzweather.service.impl;

import com.gs.wzweather.service.ServiceFileService;
import com.gs.wzweather.utils.Constant;
import com.gs.wzweather.vo.WordPageInfoVO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.io.File;
import java.util.*;

/**
 * Created with IntelliJ IDEA.
 * User: Legolas
 * Date: 2017/10/18 0018
 */
@Service
public class ServiceFileServiceImpl implements ServiceFileService {

    Logger logger = LoggerFactory.getLogger(this.getClass());

    @Override
    public WordPageInfoVO getThreeMonthlyPageInfo(Integer pageNum, String filePath) {
        Calendar calendar = Calendar.getInstance();
        filePath = filePath+ calendar.get(Calendar.YEAR)+"/";
        WordPageInfoVO info = new WordPageInfoVO();
        File file = new File(filePath);
        if(file.exists()){
            ArrayList<String> fileNames;
            String[] aimFileNames = null;
            String[] allFileNames = file.list((File dir,String name)->(name.contains(calendar.get(Calendar.YEAR)+"年")&&(name.contains("月上旬天气趋势-温州")||name.contains("月下旬天气趋势-温州"))&&name.endsWith(".doc")));
            if(allFileNames.length==0){
                info.setTotalPage(0);
                return info;
            }
            pageNum =pageNum == null ? 1:pageNum;
            int start = (pageNum-1)* Constant.WordPath.PAGE_SIZE;
            int end = pageNum* Constant.WordPath.PAGE_SIZE;
            int totalPage = allFileNames.length/ Constant.WordPath.PAGE_SIZE +(allFileNames.length% Constant.WordPath.PAGE_SIZE ==0 ? 0:1);
            String[] dirs = filePath.split("/");
            String fileDir = "";
            for (int i=1;i<dirs.length;i++){
                fileDir = fileDir + dirs[i]+"/";
            }

            info.setFirDir(fileDir);
            info.setCurrentPage(pageNum);
            info.setTotalPage(totalPage);


            if(end<=allFileNames.length){
                aimFileNames = Arrays.copyOfRange(allFileNames,start,end);
                fileNames = new ArrayList<>(Arrays.asList(aimFileNames));
                info.setFileNames(fileNames);
            }
            else{
                aimFileNames = Arrays.copyOfRange(allFileNames,start,allFileNames.length);
                fileNames = new ArrayList<>(Arrays.asList(aimFileNames));
                info.setFileNames(fileNames);
            }
            logger.info("info->{}",info);
            return info;

        }
        return new WordPageInfoVO(0);
    }

    @Override
    public WordPageInfoVO getImportantPageInfo(Integer pageNum, String filePath) {
        Calendar calendar = Calendar.getInstance();
        filePath = filePath+ calendar.get(Calendar.YEAR)+"/";
        List<String> aimFileNames;
        WordPageInfoVO info = new WordPageInfoVO();
        File file = new File(filePath);
        if(file.exists()){
            List<String> allFileNames = null;
            String[] firstFileNames = file.list((File listFile,String fileName)->(fileName.startsWith("重要气象报告（第")&&fileName.endsWith("期）.doc")));
            File[] firstDirs = file.listFiles((File listDir)->(listDir.isDirectory()));

            if(firstFileNames.length!=0) allFileNames = new ArrayList<>(Arrays.asList(firstFileNames));
            logger.info("firstAllFileNames->{}",allFileNames+""+allFileNames.size());

            if(firstDirs.length!=0)
            for (int i=0;i<firstDirs.length;i++){
                File secondFile = firstDirs[i];
                logger.info("secondFile->{}",secondFile);
                String[] secondFileNames = null;
                if(secondFile.exists())
                    secondFileNames = secondFile.list((File listFile,String fileName)->(fileName.startsWith("重要气象报告（第")&&fileName.endsWith("期）.doc")));

                if (secondFileNames!=null&&secondFileNames.length!=0)
                    for(int j=0;j<secondFileNames.length;j++){
                        logger.info("secondFileName->{}",secondFileNames[j]+""+secondFileNames.length);
                        allFileNames.add(firstDirs[i].getName()+"/"+secondFileNames[j]);
                    }
            }

            logger.info("allFileNames->{}",allFileNames);

            if(allFileNames.size()==0){
                info.setTotalPage(0);
                return info;
            }
            pageNum =pageNum == null ? 1:pageNum;
            int start = (pageNum-1)*Constant.WordPath.PAGE_SIZE;
            int end = pageNum*Constant.WordPath.PAGE_SIZE;
            int totalPage = allFileNames.size()/Constant.WordPath.PAGE_SIZE +(allFileNames.size()%Constant.WordPath.PAGE_SIZE ==0 ? 0:1);
            String[] dirs = filePath.split("/");
            String fileDir = "";
            for (int i=1;i<dirs.length;i++){
                fileDir = fileDir + dirs[i]+"/";
            }

            info.setFirDir(fileDir);
            info.setCurrentPage(pageNum);
            info.setTotalPage(totalPage);


            if(end<=allFileNames.size()){
                aimFileNames = allFileNames.subList(start,end);
                info.setFileNames(aimFileNames);
            }
            else{
                aimFileNames = allFileNames.subList(start,allFileNames.size());
                info.setFileNames(aimFileNames);
            }
            logger.info("allFileNames->{}",allFileNames);
            logger.info("info->{}",info);
            return info;

        }
        return new WordPageInfoVO(0);
    }

    @Override
    public WordPageInfoVO getPortAndChannelPageInfo(Integer pageNum, String filePath) {
        List<String> aimFileNames;
        WordPageInfoVO info = new WordPageInfoVO();
        File file = new File(filePath);
        if(file.exists()){
            String[] fileNames = file.list((File listFile,String fileName)->(fileName.endsWith(".txt")));
            List<String> allFileNames = new ArrayList<>(Arrays.asList(fileNames));
            if(allFileNames.size()==0){
                info.setTotalPage(0);
                return info;
            }
            pageNum =pageNum == null ? 1:pageNum;
            int start = (pageNum-1)*Constant.WordPath.PAGE_SIZE;
            int end = pageNum*Constant.WordPath.PAGE_SIZE;
            int totalPage = allFileNames.size()/Constant.WordPath.PAGE_SIZE +(allFileNames.size()%Constant.WordPath.PAGE_SIZE ==0 ? 0:1);

            info.setCurrentPage(pageNum);
            info.setTotalPage(totalPage);


            if(end<=allFileNames.size()){
                aimFileNames = allFileNames.subList(start,end);
                info.setFileNames(aimFileNames);
            }
            else{
                aimFileNames = allFileNames.subList(start,allFileNames.size());
                info.setFileNames(aimFileNames);
            }
            return info;
        }
        return new WordPageInfoVO(0);
    }
}
